package futian 
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import flash.geom.*;
	public class Player3x5 extends Sprite
	{
		private var stageW:int;
		//return 1920;
		private var stageH:int;
		//return 1080;
		public function Player3x5(w:int,h:int)
		{
			stageW = w;
			stageH= h;
			if(stage){
				init(null);
			}else{
				addEventListener(Event.ADDED_TO_STAGE,init);
			}
		}
		private function init(e:Event):void
		{
			if(e.target!=this || e.currentTarget!=this)
				return;
			name = ""+uint(-1);
			play(photoIndex);
		}
		private function get maxCol():int
		{
			if(Datas.jsonObject)
			{
				if(Datas.jsonObject.Player3x5.numColumns>0)
					return Datas.jsonObject.Player3x5.numColumns;
			}
			return 6;
		}
		private function get maxRow():int
		{
			if(Datas.jsonObject)
			{
				if(Datas.jsonObject.Player3x5.numRows>0)
					return Datas.jsonObject.Player3x5.numRows;
			}
			return 4;
		}
		public var pathArr:Array;
		public var photoIndex:int;

		/*
		   private function get totalTime():int
		   {
		   var maxPhotos:int = maxCol*maxRow;
		   var i:int = (maxPhotos-1) * 2000;
		   i+=10000;
		   i+=1500;
		   return i;
		   }
		 */

		private var timeoutId:uint;
		public function play(i:int):void
		{
			clearTimeout(timeoutId);
			timeoutId = setTimeout(playNext,2000);

			var maxPhotos:int = maxCol*maxRow;
			if(numChildren+1>=maxPhotos){
				freeze();
			}

			photoIndex = i;
			SwfLoader.SwfLoad(pathArr[photoIndex],loaded);
		}

		private function playNext():void
		{
			++photoIndex;
			if(pathArr==null || photoIndex>=pathArr.length)
				photoIndex = 0;
			play(photoIndex);
		}

		private function loaded(e:Event):void
		{
			if(e && Event.COMPLETE == e.type)
			{
				var bmp:Bitmap = e.target.content as Bitmap;
				if(bmp){
					var mc:Sprite = new Sprite();
					mc.name = ""+photoIndex;
					mc.addChild(bmp);
					//mc.z=0;
					bmp.x = -bmp.width/2;
					bmp.y = -bmp.height/2;
					//CONFIG::debugging
					{
						mc.width = stageW/8;
					}
					mc.scaleY = mc.scaleX;
					addChild(mc);
					showUp(mc);
				}
			}
		}

		private function showUp(mc:Sprite):void
		{
			//logs.adds(mc.name,"in");
			var i:int = numChildren-1;
			var ww:int = stageW/maxCol;
			var hh:int = stageH/maxRow*.8;
			var xx:int = 0;
			var yy:int = 0;
			var toX:int = 0;
			var toY:int = 0;
			var r:int = stageW/3;
			var angle:Number = 0;

			switch(0)
			{
				case 0:
					mc.x = - stageW - mc.width; 
					mc.y = (stageH - mc.height)*Math.random()+mc.height/2;
					xx = 0;
					yy = (stageH-hh*maxRow)/2;
					toX = xx+(int(i%maxCol)+1.0/2)*ww;
					toY = yy+(int(i/maxCol)+1.0/2)*hh;
					TweenLite.to(mc,1.0,{x:toX,rotationZ:360,y:toY});
					break;
			}

		}


		private var setTimeoutId:uint;

		private function freeze():void
		{
			clearTimeout(timeoutId);
			clearTimeout(setTimeoutId);
			var t:uint = 10000;
			CONFIG::debugging
			{
				t = 1500;
			}
			setTimeoutId = setTimeout(out,t);
		}
		private function out():void
		{
			clearTimeout(setTimeoutId);
			setTimeoutId = setTimeout(stop,1500);

			TweenLite.to(this,1.0,{y:-stageH});
		}

		private function stop():void
		{
			clearTimeout(setTimeoutId);

			while(numChildren>0)
			{
				var mc:Sprite = getChildAt(0) as Sprite;
				removeObj(mc);
			}

			var _parent:PhotoPlayer = parent as PhotoPlayer;
			if(_parent)
			{
				_parent.replay();
			}
		}
		private function removeObj(mc:Sprite):void
		{
			if(mc)
			{
				if(mc.numChildren>0)
					ViewSet.removes(mc);

				if(mc.parent)
					mc.parent.removeChild(mc);
			}
		}
	}
}
